
//表格内容
var userData = 2;
var unitId = 1;
var searchVal = '';
var page = 1;
let limit = 10;
let tableHeight = $('body').height() - 320;
let coloms = [
	[{
			field: 'PROJECT_NAME',
			title: '型号名称',
			width: 235,
			templet: function(d) {
				return `<div class="name">
				<span class="blue">${d.PROJECT_NAME.slice(0,1)}</span>
				<p>${d.PROJECT_NAME}</p>
			</div>`
			}
		},
		{
			field: 'CYC_BUDGET_DIRECT_COST',
			title: '概算直接成本',
			sort: true,
			align: 'right',
			width: 158,
			templet: function(d) {return `${pricefmt(d.CYC_BUDGET_DIRECT_COST)}`}
		},
		{
			field: 'IMPL_DIRECT_COST',
			title: '执行直接成本',
			sort: true,
			width: 158,
			align: 'right',
			templet: function(d) {return `${pricefmt(d.IMPL_DIRECT_COST)}`}
		},
		{
			field: 'rate1',
			title: '执行率',
			sort: true,
			width: 132,
			align: 'right',
			templet: function(r) {
				if(r.IMPL_DIRECT_COST == 0 || r.CYC_BUDGET_DIRECT_COST == 0){
					value =0
				}
				else{
					if(r.IMPL_DIRECT_COST == r.CYC_BUDGET_DIRECT_COST){
						value = 100
						return `<div style = 'diasplay: flex;'>
						<p style="background-color:#FD4438 ;width:72px; height:6px; border-radius: .5rem; display: inline-block;margin-bottom: 2px;"></p> &nbsp; ${value}%
						</div>`
						
					}else{
						value = (r.IMPL_DIRECT_COST/r.CYC_BUDGET_DIRECT_COST).toFixed(2) * 100
					}
					// value = (r.IMPL_DIRECT_COST/r.CYC_BUDGET_DIRECT_COST).toFixed(2) * 100
				}
				return `<progress max='100' value=${ value} style="color:#3DD598 ;width:72px"></progress> &nbsp; ${value}%`
			}
			
		},
		{
			field: 'CYC_BUDGET_INDIRECT_COST',
			title: '概算间接成本',
			sort: true,
			width: 158,
			align: 'right',
			templet: function(d) {return `${pricefmt(d.CYC_BUDGET_INDIRECT_COST)}`}
		},
		{
			field: 'IMPL_INDIRECT_COST',
			title: '执行间接成本',
			sort: true,
			width: 158,
			align: 'right',
			templet: function(d) {return `${pricefmt(d.IMPL_INDIRECT_COST)}`}
		},
		{
			field: 'rate2',
			title: '执行率',
			sort: true,
			width: 132,
			align: 'center',
			templet: function(r) {
				if(r.IMPL_INDIRECT_COST == 0 || r.CYC_BUDGET_INDIRECT_COST == 0){
					value =0
				}
				else{
					if(r.IMPL_INDIRECT_COST == r.CYC_BUDGET_INDIRECT_COST){
						value = 100
						return `<div style = 'diasplay: flex;'>
						<p style="background-color:#FD4438 ;width:72px; height:6px; border-radius: .5rem; display: inline-block;margin-bottom: 2px;"></p> &nbsp; ${value}%
						</div>`
						
					}else{
						value = (r.IMPL_INDIRECT_COST/r.CYC_BUDGET_INDIRECT_COST).toFixed(2) * 100
					}
				}
				return `<progress max='100' value=${value} style="color:#3DD598 ;width:72px"></progress> &nbsp; ${value}%`
			}
		},

	]
];

function getTableData() {
	var data = [];
	$.ajax({
			type: 'get',
			url: http + 'xhqzq',
			data: '&search=' + searchVal + '&page_num=1' + '&limit_num=' + limit,
			dataType: "json",
			async: true,
			success: function(result) {
				if (result.code == 200) {
					let data = result.data ? result.data : [];
					if(result.data){
						//直接成本执行率
					rate_num = (result.l_zxzj_sum / result.yszj_sum).toFixed(4);
					let circle = document.querySelectorAll("circle")[1];
					let perimeter = circle.getTotalLength() //圆环的周长
					circle.setAttribute('stroke-dasharray', perimeter * rate_num + " " + perimeter * (1 - rate_num));
					if(result.l_zxzj_sum / result.yszj_sum == 1){
						circle.setAttribute('stroke','#FD4438')
					}
					//间接成本执行率
					indirect_num = (result.l_zxjj_sum / result.l_ysjj_sum).toFixed(4)
					let circle1 = document.getElementById("indirect_num");
					let perimeter1 = circle1.getTotalLength() //圆环的周长
					circle1.setAttribute('stroke-dasharray', perimeter * indirect_num + " " + perimeter * (1 - indirect_num));
					if (result.l_zxjj_sum / result.l_ysjj_sum == 1){
						circle1.setAttribute('stroke','#FD4438')
					}
					$(".budget").text(pricefmt(result.yszj_sum));
					$(".implement").text(pricefmt(result.l_zxzj_sum));
					$(".rate_num").text( (rate_num * 100).toFixed(2)  + '%');
					$(".budget2").text(pricefmt(result.l_ysjj_sum));
					$(".implement2").text(pricefmt(result.l_ysjj_sum));
					$(".indirect_num").text( (indirect_num * 100).toFixed(2)  + '%');
					}
					

				// var data = result.data;
				tableDrawing(data) // 数据传到 table组件
				pagesDrawing(result) // 数据传到 分页组件
			} else {
				console.log(result);
			}
		},
		error: function() {
			console.log('访问失败');
		}
	});
}


function tableDrawing(data) {
	layui.use('table', function() {
		let table = layui.table;
		table.render({
			elem: '#tableList',
			cols: coloms,
			data: data, // 数据
			limit: limit, // 每页显示的条数
			height: tableHeight,
		});
	})
}

function pagesDrawing(data) {
	layui.use('laypage', function() {
		let laypage = layui.laypage;
		laypage.render({
			elem: 'laypage', //注意，这里的 page 是 ID，不用加 # 号
			count:data.record_total, //数据总数，从服务端得到
			theme: '#E8F3FF',
			limit: limit, // 每页条数
			prev: '<em><</em>',
			next: '<em>></em>',
			layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
			jump: function(obj, first) {
				//首次不执行
				if (!first) {
					limit = obj.limit;
					numpage(obj.curr, obj.limit) // 分页点击传参 
				}
			}
		});
	})

};

function numpage(curr, page_count) { // 分页切换-数据请求
	$.ajax({
		type: 'get',
		url:http + 'xhqzq',
		data: {
			page_num: curr,
			search: searchVal,
			limit_num: page_count
		},
		dataType: "json",
		async: true,
		success: function(result) {
			console.log(result);
			if (result.code == 200) {
				
				var data = result.data;
				tableDrawing(data) // 数据传到 table组件
			} else {
				console.log(result);
			}
		},
		error: function() {
			console.log('访问失败');
		}
	});
};


$(function() {
	
	
	getTableData()


	let isFocus = false;
	$('.wrapper_search .search_box input').focus(function() {
		isFocus = true
	})
	$('.wrapper_search .search_box input').blur(function() {
		isFocus = false
	})
	$(document).keypress(function(event) {
		let key = event.keyCode;
		searchVal = $('.wrapper_search .search_box input').val()
		if (key == '13' && isFocus) {
			getTableData()
		}
	})
	$('.wrapper_search .search_box img').click(function() {
		searchVal = $('.wrapper_search .search_box input').val()
		getTableData()
	})

})
